#pragma once 

#include <string>
#include <hash_map>
#include <iostream>

using std::string;

namespace Network
{
	namespace Peer
	{
		class Neighbour
		{
		public:
			char* m_Ip;
			char* m_port;
			int lastConnection;

			Neighbour()
			{
				m_Ip = "0.0.0.0";
				m_port = "0000";
				lastConnection = 0;
			}

			Neighbour(char* p_ipAddress, char* p_port)
			{
				m_Ip = p_ipAddress;
				m_port = p_port;
				lastConnection = 0;
			}

			Neighbour(string p_ipAddress, string p_port)
			{
				m_Ip = new char[p_ipAddress.size()];
				m_port = new char[p_port.size()];

				std::strcpy(m_Ip, p_ipAddress.c_str());
				std::strcpy(m_port , p_port.c_str());
				
				lastConnection = 0;
			}

			Neighbour(char* p_ipAddress, char* p_port, int _logicalTime)
			{
				m_Ip = p_ipAddress;
				m_port = p_port;
				lastConnection = _logicalTime;
			}

			Neighbour(const char* p_ipAddress,const char* p_port, int _logicalTime)
			{
				m_Ip = new char[sizeof(p_ipAddress)];
				m_port = new char[sizeof(p_port)];
				std::strcpy(m_Ip,p_ipAddress);
				std::strcpy(m_port, p_port);
				lastConnection = _logicalTime;
			}

			Neighbour(string p_neighbourDetails)
			{
				lastConnection = 0;
				FromString(p_neighbourDetails);
			}

			Neighbour(const Neighbour& p_neighbour)
			{
				strcpy(m_Ip, p_neighbour.m_Ip);
				strcpy(m_port, p_neighbour.m_port);
				lastConnection = p_neighbour.lastConnection;
			}

			void UpdateLogicalTime(int time)
			{
				lastConnection = time;
			}

			void IncrementLogicalTime()
			{
				lastConnection++;
			}

			std::string ToString()
			{
				return std::string(m_Ip) + "-" + std::string(m_port) + "-" + std::to_string(lastConnection);
			}
			void FromString(std::string p_data)
			{
				std::string data[3];
				HelperClass::ParseStringSentence(p_data, "-", data);

				m_Ip = new char[data[0].size()];
				m_port = new char[data[1].size()];
				
				std::strcpy(m_Ip, data[0].c_str());
				std::strcpy(m_port, data[1].c_str());
				lastConnection = stoi(data[2]);
			}

			bool operator < (const Neighbour& neighbour)
			{
				return lastConnection < neighbour.lastConnection;
			}

			bool operator >(const Neighbour& neighbour)
			{
				return lastConnection > neighbour.lastConnection;
			}

			bool SortFunction(const Neighbour lhs, const Neighbour rhs)
			{
				return lhs.lastConnection > rhs.lastConnection;
			}

			bool operator == (const Neighbour& rhs) const
			{
				return GetNeigbourRepresentation() == rhs.GetNeigbourRepresentation();
			}

			string GetNeigbourRepresentation() const
			{
				return std::string(m_Ip) + ":" + std::string(m_port);
			}

			Neighbour& operator = (const Neighbour& p_neighbour)
			{
				std::strcpy(m_Ip, p_neighbour.m_Ip);
				std::strcpy(m_port, p_neighbour.m_port);
				lastConnection = p_neighbour.lastConnection;
				return *this;
			}


			Neighbour& operator = (Neighbour* p_neighbour)
			{

				std::strcpy(m_Ip, p_neighbour->m_Ip);
				std::strcpy(m_port, p_neighbour->m_port);
				lastConnection = p_neighbour->lastConnection;
				return *this;
			}

		};

		
		
	}
}


